package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.FormatUtil;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.util.ABRegUtil;
import com.eeepay.v2_library.view.HorizontalItemView;
import com.eeepay.v2_library.view.LabelEditText;
import com.eeepay.v2_library.view.TitleBar;

/**
 * Created by zw on 2016/3/8 0008.
 * 机具管理
 */
public class DeviceManageActivity extends ABBaseActivity implements View.OnClickListener {

    private TitleBar titleBar;
    //机具SN号、 终端号、   PSAM号  、 商户名称 、 所分配代理商名称
    private LabelEditText et_SN, et_terminal, et_PSAM, et_merchantName, et_agentName;
    private TextView[] state_select = new TextView[2];//2种状态
    private HorizontalItemView tv_deviceType,tv_activity_type;//add by zhuangzeqin 2017年6月22日16:50:50 增加活动类型
    private Button btn_confirm;

    private String deviceStatus;//机具状态
    private String deviceTypeIds = "";//机具状态
    private String activity_value="";//add by zhuangzeqin 2017年8月21日18:17:31 活动类型对应的值

    private final int snLength = 16;//SN号的长度为16

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_manage;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
        titleBar.setShowRight(View.VISIBLE);
        titleBar.setRightTextView("重置");
        titleBar.setRightTextColor(R.color.unify_grounding_white);

        state_select[0] = getViewById(R.id.tv_select_1);
        state_select[1] = getViewById(R.id.tv_select_2);
        //机具SN号、 终端号
        et_SN = getViewById(R.id.et_sn);
        et_terminal = getViewById(R.id.et_terminal);
        et_PSAM = getViewById(R.id.et_psam);
        et_merchantName = getViewById(R.id.et_merchant_name);
        et_agentName = getViewById(R.id.et_agent_name);

        tv_deviceType = getViewById(R.id.tv_device_type);
        //add by zhuangzeqin 2017年6月22日16:46:14 增加活动类型
        tv_activity_type = getViewById(R.id.tv_activity_type);
        btn_confirm = getViewById(R.id.btn_confirm);
    }

    @Override
    protected void eventOnClick() {
        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                reset();
            }
        });

        for (TextView tv: state_select) {
            tv.setOnClickListener(this);
        }

        tv_deviceType.setOnClickListener(this);
        tv_activity_type.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);

        //机具SN号和终端号不能输入中文---
        et_SN.getEditText().setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus==false) {
                    if (ABRegUtil.isRegiest(et_SN.getEditContent(), ABRegUtil.REG_STRING)) {
                        showToast("机具SN号不包括中文字符,请重新输入");
                        et_SN.getEditText().setText("");
                        et_SN.getEditText().clearFocus();
                        return;
                    }
                    else if (FormatUtil.checkStringContainChinese(et_SN.getEditContent()))
                    {
                        showToast("机具SN号不包括中文字符,请重新输入");
                        et_SN.getEditText().setText("");
                        et_SN.getEditText().clearFocus();
                        return;
                    }
                    //sn 号判断长度
                    else if (!TextUtils.isEmpty(et_SN.getEditContent()) && et_SN.getEditContent().length() != snLength) {
                        showToast("请填写正确的SN号");
                        et_SN.getEditText().setText("");
                        et_SN.getEditText().clearFocus();
                        return;
                    }
                }
            }
        });
        //端号不能输入中文
        et_terminal.getEditText().setOnFocusChangeListener(new View.OnFocusChangeListener(){
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus==false) {
                    if (ABRegUtil.isRegiest(et_terminal.getEditContent(), ABRegUtil.REG_STRING)) {
                        showToast("终端号不包括中文字符,请重新输入");
                        et_terminal.getEditText().setText("");
                        et_terminal.getEditText().clearFocus();
                        return;
                    }
                    else if (FormatUtil.checkStringContainChinese(et_terminal.getEditContent()))
                    {
                        showToast("终端号不包括中文字符,请重新输入");
                        et_terminal.getEditText().setText("");
                        et_terminal.getEditText().clearFocus();
                        return;
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_select_1:
                changeSelected(0);
                break;
            case R.id.tv_select_2:
                changeSelected(1);
                break;
            case R.id.tv_device_type:
                goActivityForResult(PosTypeActivity.class,  Constant.RequestCode.device_all);
                break;
            case R.id.tv_activity_type://add by zhuangzeqin 2017年6月22日17:09:03 新增活动类型
                Bundle bundle = new Bundle();
                bundle.putString(Constant.INTENT_FLAG, Constant.ACTIVITY_TYPE);
                goActivityForResult(ChooseActivity.class,bundle, Constant.RequestCode.ACTIVITY_TYPE);
                break;
            case R.id.btn_confirm:
                queryResult();

                break;
        }
    }

    /**
     * 统计查询条件
     */
    private void queryResult() {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.DEVICE_SN, et_SN.getEditContent());
        bundle.putString(Constant.TERMINAL_NUMBER, et_terminal.getEditContent());
        bundle.putString(Constant.PSAM_NUMBER, et_PSAM.getEditContent());
        bundle.putString(Constant.DEVICE_STATUS, deviceStatus);
        bundle.putString(Constant.DEVICE_TYPE, (deviceTypeIds == null ?"":deviceTypeIds));
        bundle.putString(Constant.MERCHANT_NAME, et_merchantName.getEditContent());
        bundle.putString(Constant.AGENT_NAME, et_agentName.getEditContent());
        //add by zhuangzeqin 2017年6月22日17:49:38 新增活动类型
        bundle.putString(Constant.ACTIVITY_TYPE, activity_value);
        goActivity(DeviceListActivity.class, bundle);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != RESULT_OK){
            return;
        }
        if(data != null){
            switch (requestCode){
                case Constant.RequestCode.device_all:
                    boolean isAll = data.getBooleanExtra("isAll", false);
                    if(isAll){
                        this.tv_deviceType.setRightText("全部");
                        deviceTypeIds = "";
                    }else{
                        String device = data.getStringExtra("device_type");
                        deviceTypeIds = data.getStringExtra("device_ids");
                        this.tv_deviceType.setRightText(TextUtils.isEmpty(device)?"请选择":device);
                    }
                    break;
                case Constant.RequestCode.ACTIVITY_TYPE://机具活动类型选择
                    String activity_type = data.getStringExtra(Constant.ACTIVITY_TYPE);
                    activity_value = data.getStringExtra(Constant.ACTIVITY_VALUE);//add by zhuangzeqin 2017年8月21日18:18:08 新增活动类型对应的的值
                    this.tv_activity_type.setRightText(activity_type);
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 改变选择框的选中状态
     * @param num
     */
    private void changeSelected(int num) {
        for(int i = 0; i < state_select.length; i++){
            if(i == num){
                if(state_select[i].isSelected()){
                    state_select[i].setSelected(false);
                    deviceStatus = "";
                }else{
                    state_select[i].setSelected(true);
                    if(i == 0){
                        deviceStatus = "2";
                    }else if(i == 1){
                        deviceStatus = "1";
                    }
                }
            }else{
                state_select[i].setSelected(false);
            }
        }
    }

    private void reset(){
        et_SN.setEditContent("");
        et_terminal.setEditContent("");
        et_PSAM.setEditContent("");
        et_merchantName.setEditContent("");
        et_agentName.setEditContent("");
        tv_deviceType.setRightText("请选择");
        tv_activity_type.setRightText("请选择");
    }


}
